package com.egg.ylt.presenter.impl;

import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.pojo.GrowUpRecordEntity;
import com.egg.ylt.presenter.IGrowRecordPresenter;
import com.egg.ylt.view.IGrowRecordView;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.util.HashMap;

/**
 * Created by lijiateng on 2019/1/19.
 */

public class GrowRecordPresenterImpl extends BasePresenter<IGrowRecordView> implements IGrowRecordPresenter {

    public void getGrowUpRecordList(String babyId, int pageNum) {

        mView.showDialogLoading("正在加载中...");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("companyId", Constants.COMPANYID);
        hashMap.put("babyId", StringUtil.get(babyId));
        hashMap.put("pageSize", StringUtil.get("20"));
        hashMap.put("pageNum", StringUtil.get(pageNum));
        RequestManager.getInstance().requestPostByAsyn(API.POST_GROW_UP_RECORD, hashMap, new ReqCallBack<GrowUpRecordEntity>() {
            @Override
            public void onReqSuccess(GrowUpRecordEntity result) {
                mView.dismissDialogLoading();
                mView.updateGrowRecordList(result.getList());
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                mView.showError(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                mView.showError(error.getMsg());
            }
        });
    }

    /**
     * 删除当前条目
     *
     * @param babyId
     * @param itemId
     */
    public void doDeleteCurrItem(String babyId, String itemId) {

        mView.showDialogLoading("正在加载中...");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("companyId", Constants.COMPANYID);
        hashMap.put("babyId", StringUtil.get(babyId));
        hashMap.put("id", StringUtil.get(itemId));
        RequestManager.getInstance().requestGetByAsyn(API.GET_DELETE_RECORD, hashMap, new ReqCallBack<String>() {
            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
                mView.deleteItemSucceed();
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                mView.showError(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                mView.showError(error.getMsg());
            }
        });
    }
}
